import React from 'react';
import { WingBlank, WhiteSpace, List, Flex, Toast } from 'antd-mobile';
import { connect } from 'dva';
import { Link, routerRedux } from 'dva/router';

const ScaleMemberPage = ({ dispatch, location, members, ...rest }) => {
  document.body.scrollTop = document.documentElement.scrollTop = 0;
  const goto = (member) => {
    // 重置model数据
    dispatch({ type: 'scale/reset' });
    const newdate = new Date();
    const newyear = newdate.getFullYear();
    const oldyear = member.birthday.substr(0,4)*1;
    const Age = newyear - oldyear;
    const partumbirth = member.birthday.split(" ")[0];
   if(location.pathname=="/healthy/diagnosis/family"){
       dispatch(
        routerRedux.push({
          pathname: `${location.pathname}/${member.patientId}`,
          state: {
            patientid: member.patientId,
            memberid: member.familyId,
            gender: member.gender,
          },
        }),
      );
    }else{
      if(location.state){
          const { type } = location.state;
          if (type === 'phy') {
              dispatch(
                routerRedux.push({
                  pathname: `${location.pathname}/${member.patientId}/phy`,
                  state: {
                    type,
                    patientid: member.patientId,
                    familyId: member.familyId,
                    step: 0,
                  },
                }),
              );
          } else if (type === 'phy2') {
            dispatch(
              routerRedux.push({
                pathname: `${location.pathname}/${member.patientId}/phy2`,
                state: {
                  type,
                  patientid: member.patientId,
                  familyId: member.familyId,
                  step: 0,
                },
              }),
            );
        } else if (type === 'pre') {
            if (member.gender == 1 ) {
              Toast.fail("对不起，您选的成员必须是女性", 1);
              return ;
            }
            if ( Age <7) {
              Toast.fail("对不起，您选的成员应不小于7岁",1);
              return ;
            }
            dispatch(
              routerRedux.push({
                pathname: `${location.pathname}/${member.patientId}/pre`,
                state: {
                  type,
                  patientid: member.patientId,
                  familyId: member.familyId,
                  step: 1,
                },
              }),
            );
          } else if (type === 'hyp') {
            if ( Age < 55) {
              Toast.fail("对不起，您选的成员应不小于55岁",1);
              return ;
            }
            dispatch(
              routerRedux.push({
                pathname: `${location.pathname}/${member.patientId}/hyp`,
                state: {
                  type,
                  patientid: member.patientId,
                  familyId: member.familyId,
                  step: 1,
                },
              }),
            );
          } else if (type === 'chd') {
            if ( Age > 6) {
              Toast.fail("对不起，您选的成员应不大于6岁",1);
              return ;
            }
            dispatch(
              routerRedux.push({
                pathname: `${location.pathname}/${member.patientId}/chd`,
                state: {
                  type,
                  patientid: member.patientId,
                  familyId: member.familyId,
                  gender: member.gender,
                  step: 1,
                },
              }),
            );
          } else if (type === 'old') {
            // old 和 nine 默认路径，默认Template
            if (Age < 65) {
              Toast.fail("对不起，您选的成员应不小于65岁",1);
              return ;
            }
            dispatch(
              routerRedux.push({
                pathname: `${location.pathname}/${member.patientId}`,
                state: {
                  type,
                  patientid: member.patientId,
                  familyId: member.familyId,
                },
              }),
            );
          } else if (type === 'tcm') {
            if (Age < 18) {
              Toast.fail("对不起，您选的成员应不小于18岁",1);
              return ;
            }
            dispatch(
              routerRedux.push({
                pathname: `${location.pathname}/${member.patientId}/tcm`,
                state: {
                  type,
                  patientid: member.patientId,
                  familyId: member.familyId,
                  gender: member.gender,
                  step: 1
                },
              }),
            );
          } else if (type === 'dia') {
            if (Age < 18) {
              Toast.fail("对不起，您选的成员应不小于18岁",1);
              return ;
            }
            dispatch(
              routerRedux.push({
                pathname: `${location.pathname}/${member.patientId}/dia`,
                state: {
                  type: 'dia',
                  patientid: member.patientId,
                  familyId: member.familyId,
                  gender: member.gender,
                  step: 1
                },
              }),
            );
          } else if (type === 'partum') {
            if (member.gender == 1 ) {
              Toast.fail("对不起，您选的成员必须是女性", 1);
              return ;
            }
            if ( Age <7) {
              Toast.fail("对不起，您选的成员应不小于7岁",1);
              return ;
            }
            dispatch(
              routerRedux.push({
                pathname: `${location.pathname}/${member.patientId}/partum`,
                state: {
                  type,
                  patientid: member.patientId,
                  familyId: member.familyId,
                  step: 1,
                  birthday: partumbirth,
                },
              }),
            );
          } else if (type === 'prepre'){
            if (Age < 18) {
              Toast.fail("对不起，您选的成员应不小于18岁",1);
              return ;
            }
            if(Age > 56){
              Toast.fail("对不起，您选的成员应不大于56岁",1);
              return ;
            }
            if (member.gender == 1 ) {
              Toast.fail("对不起，您选的成员必须是女性", 1);
              return ;
            }
            dispatch(
              routerRedux.push({
                pathname: `${location.pathname}/${member.patientId}/prepre`,
                state: {
                  type,
                  patientid: member.patientId,
                  familyId: member.familyId,
                  step: 1,
                  birthday: member.birthday,
                },
              }),
            );
          }else if ( type === 'normal') {
            if (Age < 18 ) {
              Toast.fail("对不起，您选的成员应不小于18岁",1);
              return ;
            }
            if(Age > 56){
              Toast.fail("对不起，您选的成员应不大于56岁",1);
              return ;
            }
            if (member.gender == 1 ) {
              Toast.fail("对不起，您选的成员必须是女性", 1);
              return ;
            }
            dispatch(
              routerRedux.push({
                pathname: `${location.pathname}/${member.patientId}/normal`,
                state: {
                  type,
                  patientid: member.patientId,
                  familyId: member.familyId,
                  step: 1,
                  birthday: member.birthday,
                },
              }),
            );
          }else {
            // old 和 nine 默认路径，默认Template
            sessionStorage.setItem("gender",member.Gender);
            if(Age<18){
              Toast.fail("对不起，您选的成员应不小于18岁",1);
              return ;
            }
            if( Age>64){
              Toast.fail("对不起，您选的成员应不大于64岁",1);
              return ;
            }
            dispatch(
              routerRedux.push({
                pathname: `${location.pathname}/${member.patientId}`,
                state: {
                  type,
                  patientid: member.patientId,
                  familyId: member.familyId,
                  gender: member.gender,
                },
              }),
            );
          }
      }
  }
};

  return (
    <WingBlank size="md">
      <WhiteSpace />
      <List>
        {members.map(member => (
          <List.Item key={member.id} arrow="horizontal" extra={`(${member.relation})`} onClick={() => goto(member)}>
            {member.name}
          </List.Item>
        ))}
      </List>
      <WhiteSpace size="xl" />
      <Flex justify="end">
        <Link
          to={{
            pathname: '/settings/family',
          }}
        >
          管理家庭组
        </Link>
      </Flex>
    </WingBlank>
  );
};

export default connect(({ family }) => ({ ...family }))(ScaleMemberPage);
